# coding: utf-8

import tornado.gen
import json
from bson import ObjectId
from handlers.base import BaseHandler

class SuggestHandler(BaseHandler):
    @tornado.gen.coroutine
    def get(self, *args, **kwargs):
        try:
            measureId = self.get_query_argument('measureId')
            measure = yield self.db.HMMeasure.find_one({'_id': ObjectId(measureId)}, {'role': 0, '_id': 0})
            measureType = measure['measureType']
        except Exception as err:
            print(err)
            self.response_msg(400, '参数错误')
            return
        # 判断测量数据属于哪种类型
        if measureType == 'bmi':
            try:
                advice = yield self.db.HMMeasureAdvice.find_one({'_id': ObjectId(measure['advice'])}, {'_id': 0})
                measure = self.formart(measure)
                advice = self.formart(advice)
                rangeStr = '(成年人正常范围:18.5~24.99)'
            except Exception as err:
                print(err)
                self.response_msg(400, '查询错误')
        elif measureType == 'sugar':
            try:
                advice = yield self.db.HMMeasureAdvice.find_one({'_id': ObjectId(measure['advice'])}, {'_id': 0})
                timePoint = measure['timePoint']
                str_range = ''
                if timePoint == '空腹':
                    str_range = '(空腹血糖(早餐前)正常范围：3.9~6.1mmol/L)'
                elif timePoint == '餐后1小时':
                    str_range = '(餐后1小时正常范围：<11.1mmol/L)'
                elif timePoint == '餐后2小时':
                    str_range = '(餐后2小时正常范围：<7.8mmol/L)'
                elif timePoint == '其他时间':
                    str_range = '(其他时间正常范围：2.8~7.8mmol/L)'
                measure = self.formart(measure)
                rangeStr = str_range
            except Exception as err:
                print(err)
                self.response_msg(400, '查询错误')
        elif measureType == 'pressure':
            try:
                advice = yield self.db.HMMeasureAdvice.find_one({'_id': ObjectId(measure['advice'])}, {'_id': 0})
                advice = self.formart(advice)
                measure = self.formart(measure)
                rangeStr = '(正常范围:高压90~130mmHg 低压60~85mmHg)'
            except Exception as err:
                print(err)
                self.response_msg(400, '查询错误')
        self.response_msg(200, 'success',{'measure': measure, 'advice': advice, 'range': rangeStr})




